package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.excel.ImportExcelUtil;
import com.ruoyi.system.domain.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TmcPeeMapper;
import com.ruoyi.system.service.ITmcPeeService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-31
 */
@Service
public class TmcPeeServiceImpl implements ITmcPeeService {
    @Autowired
    private TmcPeeMapper tmcPeeMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public TmcPee selectTmcPeeById(Long id) {
        TmcPee tmcPee = tmcPeeMapper.selectTmcPeeById(id);
        tmcPee.setCompanyName(tmcPeeMapper.queryCompanyByName(tmcPee.getCompanyId()));
        return tmcPee;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tmcPee 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TmcPee> selectTmcPeeList(TmcPee tmcPee) {
        List<TmcPee> tmcPees = tmcPeeMapper.selectTmcPeeList(tmcPee);
        for (int i = 0; i < tmcPees.size(); i++) {
            tmcPees.get(i).setCompanyName(tmcPeeMapper.queryCompanyByName(tmcPees.get(i).getCompanyId()));
        }
        return tmcPees;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tmcPee 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTmcPee(TmcPee tmcPee) {
        if (tmcPeeMapper.querySumByid(tmcPee.getCompanyId(), 0L) == 0) {
            return tmcPeeMapper.insertTmcPee(tmcPee);
        } else {
            return 0;
        }
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tmcPee 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTmcPee(TmcPee tmcPee) {
        if (tmcPeeMapper.querySumByid(tmcPee.getCompanyId(), tmcPee.getId()) == 0) {
            return tmcPeeMapper.updateTmcPee(tmcPee);
        } else {
            return 0;
        }
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTmcPeeByIds(Long[] ids) {
        return tmcPeeMapper.deleteTmcPeeByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTmcPeeById(Long id) {
        return tmcPeeMapper.deleteTmcPeeById(id);
    }



    //计算物业费
    @Override
    public int pay(TmcPay tmcPay) {
        if (tmcPay.getCompanyName() != null & !"".equals(tmcPay.getCompanyName())) {
//            在账单表插入一条数据拿到他的id
            TmcHouseBill tmcHouseBill = new TmcHouseBill();
            //小区id
            tmcHouseBill.setCompanyId(tmcPeeMapper.queryCompanyByid(tmcPay.getCompanyName()));
            //房屋id
            tmcHouseBill.setHouseinfoId(tmcPeeMapper.queryHouseByid(tmcPay.getUnitName(), tmcPay.getHouseName()));
            //户主id
            tmcHouseBill.setPublicuserId(tmcPeeMapper.queryUserByid(tmcPay.getCellPhone()+""));
            //创建一条总账单
            tmcPeeMapper.insertTmcHouseBill(tmcHouseBill);
            //拿到总账单id
            Long id = tmcHouseBill.getId();
            //获取计费方式
            //获取小区ID
            int Id = tmcPeeMapper.selectCompany(tmcPay.getCompanyName());
            //创建子帐单对象
            TmcHouseSubbill tmcHouseSubbill = new TmcHouseSubbill();
            //关联总账
            tmcHouseSubbill.setParentbillId(id);
            //账单总额
            Double sum = 0.0;
            TmcPee tmcPee = tmcPeeMapper.getPee(Id);
            if (0 != tmcPay.getArea() && tmcPay.getArea() != 0 && 0 != tmcPee.getProperty() && tmcPee.getProperty() != 0) {
                //基本物业费
                tmcPay.setProperty(tmcPay.getArea() * tmcPee.getProperty());
                sum += tmcPay.getProperty();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("基本物业费");
                tmcHouseSubbill.setSubbillFee(tmcPay.getProperty());
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }
            if (0 != tmcPay.getWater() && tmcPay.getWater() != 0 && 0 != tmcPee.getTrush() && tmcPee.getTrush() != 0) {
                tmcPay.setTrush(tmcPee.getTrush() + 0.0);
                sum += tmcPay.getTrush();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("垃圾费");
                tmcHouseSubbill.setSubbillFee(tmcPay.getTrush());
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }
            //电费
            if (0 != tmcPay.getElectricity() && tmcPay.getElectricity() != 0 && 0 != tmcPay.getElectricity() && tmcPay.getElectricity() != 0) {
                tmcPay.setElectricity(tmcPay.getElectricity() * tmcPee.getElectricity());
                sum += tmcPay.getElectricity();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("电费");
                tmcHouseSubbill.setSubbillFee(tmcPay.getElectricity());
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }
            //水费
            if (0 != tmcPay.getWater() && tmcPay.getWater() != 0 && 0 != tmcPee.getWater() && tmcPee.getWater() != 0) {
                tmcPay.setWater(tmcPay.getWater() * tmcPee.getWater());
                sum += tmcPay.getWater();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("水费");
                tmcHouseSubbill.setSubbillFee(tmcPay.getWater());
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }

            //电梯维护费
            if (0 != tmcPay.getElevatorMaintenance() && tmcPay.getElevatorMaintenance() != 0 && 0 != tmcPee.getElevatorMaintenance() && tmcPee.getElevatorMaintenance() != 0) {
                tmcPay.setElevatorMaintenance(Math.ceil(tmcPay.getElevatorMaintenance() / tmcPee.getElevatorMaintenance()));
                sum += tmcPay.getElevatorMaintenance();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("电梯维护费");
                tmcHouseSubbill.setSubbillFee(tmcPay.getElevatorMaintenance());
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }

            //电梯电费
            if (0 != tmcPay.getElevatorElectricity() && tmcPay.getElevatorElectricity() != 0 && 0 != tmcPee.getElevatorElectricity() && tmcPee.getElevatorElectricity() != 0) {
                tmcPay.setElevatorElectricity(Math.ceil(tmcPay.getElevatorElectricity() / tmcPee.getElevatorElectricity()));
                sum += tmcPay.getElevatorElectricity();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("电梯电费");
                tmcHouseSubbill.setSubbillFee(tmcPay.getElevatorElectricity());
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }

            //电梯年检费
            if (0 != tmcPay.getElevatorAnnualInspection() && tmcPay.getElevatorAnnualInspection() != 0 && 0 != tmcPee.getElevatorAnnualInspection() && tmcPee.getElevatorAnnualInspection() != 0) {
                tmcPay.setElevatorAnnualInspection(Math.ceil(tmcPay.getElevatorAnnualInspection() / tmcPee.getElevatorAnnualInspection()));
                sum += tmcPay.getElevatorAnnualInspection();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("电梯年检费");
                tmcHouseSubbill.setSubbillFee(Math.ceil(tmcPay.getElevatorAnnualInspection()));
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }

            //小区均摊费用
            if (0 != tmcPay.getCommunity() && tmcPay.getCommunity() != 0 && 0 != tmcPee.getCommunity() && tmcPee.getCommunity() != 0) {

                tmcPay.setCommunity(Math.ceil(tmcPay.getCommunity() / tmcPee.getCommunity()));
                sum += tmcPay.getCommunity();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("小区均摊费用");
                tmcHouseSubbill.setSubbillFee(Math.ceil(tmcPay.getCommunity()));
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }
            //车库月租
            if (0 != tmcPay.getPackingNum() || tmcPay.getPackingNum() != 0 && 0 != tmcPee.getGarage() || tmcPee.getGarage() != 0) {
                tmcPay.setGarage(tmcPay.getPackingNum() * tmcPee.getGarage() + 0.0);
                sum += tmcPay.getGarage();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("车库月租");
                tmcHouseSubbill.setSubbillFee(Math.ceil(tmcPay.getGarage()));
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }
            //其他维修费用
            if (0 != tmcPay.getOther() && tmcPay.getOther() != 0 && 0 != tmcPee.getOther() && tmcPee.getOther() != 0) {
//                零做除数会得到无穷大
                tmcPay.setOther(Math.ceil(tmcPay.getOther()/tmcPee.getOther()));
                sum += tmcPay.getOther();
                //新增一条子账单
                tmcHouseSubbill.setSubbillname("其他维修费用");
                tmcHouseSubbill.setSubbillFee(tmcPay.getOther());
                tmcPeeMapper.insertTmcHouseSubbill(tmcHouseSubbill);
            }
            tmcPay.setSum(Math.ceil(sum));
            //计费完成修改总账单
            TmcHouseBill tmcHouseBill1 = new TmcHouseBill();
            tmcHouseBill1.setId(id);
            tmcHouseBill1.setTotalFee(Math.ceil(sum));
            tmcPeeMapper.updateTmcHouseBill(tmcHouseBill1);
            tmcPay.setHouseBillId(id);
            return tmcPeeMapper.insertTmcPayManage(tmcPay);
        }
        return 0;
    }

    @Override
    public int importExcel(MultipartFile file) {
        if (tmcPeeMapper.queryPaySum() == 0) {
            String fileName = file.getName();
            List<String[]> dataList = null;
            String headData = null;
            Workbook workbook = null;
            String dateFormat = "yyyy-MM-dd";
            boolean t = true;
            int a = 0;
            try {
                //部门
                workbook = new XSSFWorkbook(file.getInputStream());
                if (workbook != null) {
                    headData = ImportExcelUtil.getExcelHeadData(workbook);
                    dataList = ImportExcelUtil.getExcelData(workbook, 0, 0, dateFormat);
                }
                //拿到数据进行数据库操作
                SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
                Map map = new HashMap();
                for (int j = 1; j < dataList.size(); j++) {
                    TmcPay entity = new TmcPay();
                    String[] strings = dataList.get(j);
                    for (int i = 0; i < 17; i++) {
                        String cellValue = dataList.get(j)[i] + "";

                        if (!"null".equals(cellValue)) {
                            t = true;
                            switch (i) {
                                case 1:
                                    entity.setUserName(cellValue);
                                    if ("null".equals(cellValue)) return 0;
                                    break;
                                case 2:
                                    entity.setCellPhone(Long.valueOf(cellValue));
                                    break;
                                case 3:
                                    entity.setCompanyName(cellValue);
                                    break;
                                case 4:
                                    entity.setHouseName(cellValue);
                                    break;
                                case 5:
                                    entity.setUnitName(cellValue);
                                    break;
                                case 6:
                                    entity.setArea(Double.valueOf(cellValue));
                                    break;
                                case 7:
                                    if (null == Integer.valueOf(cellValue) || "null".equals(cellValue) || "".equals(cellValue)) {
                                        entity.setPackingNum(0);
                                    } else {
                                        entity.setPackingNum(Integer.valueOf(cellValue));
                                    }
                                    break;
                                case 8:
                                    entity.setElectricity(Double.valueOf(cellValue));
                                    break;
                                case 9:
                                    entity.setWater(Double.valueOf(cellValue));
                                    break;
                                case 10:
                                    entity.setElevatorMaintenance(Double.valueOf(cellValue));
                                    break;
                                case 11:
                                    entity.setElevatorElectricity(Double.valueOf(cellValue));
                                    break;
                                case 12:
                                    entity.setElevatorAnnualInspection(Double.valueOf(cellValue));
                                    break;
                                case 13:
                                    entity.setCommunity(Double.valueOf(cellValue));
                                    break;
                                case 14:
                                    entity.setTemporaryParking(Double.valueOf(cellValue));
                                    break;
                                case 15:
                                    Double aDouble = Double.valueOf(cellValue);
                                    entity.setOther(aDouble);
                                    break;
                                default:
                                    break;
                            }

                        }
                    }
                    pay(entity);

                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
            return 1;
        } else {

            return 0;
        }
    }

    @Override
    public List<Company> querycompanyList() {
        return tmcPeeMapper.querycompanyList();
    }

    @Override
    public int delpay() {
        return tmcPeeMapper.delpay();
    }
}
